import java.io.*;
import java.net.Socket;
import java.util.HashMap;

/**
 * Handles incoming clients
 * @author Henri 
 */

public class ClientHandler implements Runnable {

	private InputStream inputStream;
	private Socket socket;
	private MySorter sorter;
	private HashMap<Integer, Integer> stations;
	private HistoryData history;

	public ClientHandler(Socket sock, MySorter sorter, HashMap<Integer, Integer> stations, HistoryData history) throws IOException {
		socket = sock;
		inputStream = socket.getInputStream();
		
		this.sorter = sorter;
		this.stations = stations;
		this.history = history;
	}

	@Override
	public void run() {		
		MyByteParser byteParser = new MyByteParser(sorter, stations, history);		
		 
		while(true) {
			try {
				int read = inputStream.read();
				if(read != -1){
					byteParser.process(inputStream);
				}
			} catch (IOException e) {			
				e.printStackTrace();
			}
		}
	}
}
